class HuiBian2JiQi:
    def __init__(self,src):
        self.src=src
        self.i=0
        self.leng = len(src)
        # 操作数
        self.num=0
        self.nums=[]
        # 寄存器
        self.ji=[]
        # 行为
        self.xing=""
        # 指令码
        self.MC=""
        # 机器码
        self.BC=""
        # 操作数的二进制
        self.BN=""
        # 对源码抽象
        self.tokens=""
    def getBN(self,num,is_buma):
        num=int(num)
        if is_buma:
            num = 256-num
        BN=""
        BN=BN+str(num%2)
        num=int(num/2)
        BN=BN+str(num%2)
        num=int(num/2)
        BN=BN+str(num%2)
        num=int(num/2)
        BN=BN+str(num%2)
        num=int(num/2)
        BN=BN+str(num%2)
        num=int(num/2)
        BN=BN+str(num%2)
        num=int(num/2)
        BN=BN+str(num%2)
        num=int(num/2)
        BN=BN+str(num%2)
        self.BN = BN[::-1]
        return self.BN

    # 获取机器码 
    def getJiqi(self):
        print(self.tokens)
        self.BN = self.getBN(self.num,is_buma=False) 
        self.MC="00000000"
        if self.tokens=="放数入寄行":
            if len(self.ji)==1 and self.ji[0]=="A":
                self.MC="00000010"
            if len(self.ji)==1 and self.ji[0]=="B":
                self.MC="00001010"
            if len(self.ji)==1 and self.ji[0]=="BX":
                self.MC="00001011"
        if self.tokens=="寄入寄行":
            if len(self.ji)==2:
                if self.ji[0]=="A" and self.ji[1]=="B": 
                    self.MC="00000011"
                if self.ji[0]=="A" and (self.ji[1]=="BX" or self.ji[1]=="Bx"): 
                    self.MC="00010001"
                if self.ji[0]=="B" and self.ji[1]=="A": 
                    self.MC="00000100"
                if (self.ji[0]=="BX" or self.ji[0]=="Bx") and self.ji[1]=="A": 
                    self.MC="00001110"
        if self.tokens=="寄入内寄行":
            if len(self.ji)==2:
                if self.ji[0]=="A" and self.ji[1]=="BX":
                    self.MC="00001100"
        if self.tokens=="内寄入寄行":
            if len(self.ji)==2:
                if self.ji[0]=="BX" and self.ji[1]=="A":
                    self.MC="00001101"
        if self.tokens=="内数入寄行":
            if len(self.ji)==1:
                if self.ji[0]=="A":
                    self.MC="00001111"
                if self.ji[0]=="B":
                    self.MC="00000000"
        if self.tokens=="寄入内数行":
            if len(self.ji)==1:
                if self.ji[0]=="A":
                    self.MC="00010000"
        if self.tokens=="跳数行":
            self.MC="00000110"
        if self.tokens=="如寄小寄跳数行":
            self.MC="00000111"
        if self.tokens=="如寄等寄跳数行":
            self.MC="00001000"
        if self.tokens=="如寄大寄跳数行":
            self.MC="00001001"
        if self.tokens=="寄加数行":
            if len(self.ji)==1 and self.ji[0]=="A":
                self.MC="00000101"
        if self.tokens=="寄减数行":
            if len(self.ji)==1 and self.ji[0]=="A":
                self.MC="00000101"
                self.BN = self.getBN(self.num,is_buma=True) 
        return self.MC,self.BN

    # 获取下一个字符
    def getc(self):
        if self.i<self.leng:
            self.i = self.i+1
            c = self.src[self.i -1]
            #print(c)
            return c
        else:
            return "完"
    
    # 对源码抽象
    def getToken(self):
        c= self.getc()
        if c=="=":
           return "等"
        if c=="i" or c=="j":
           return "量"
        if c=="花" or c=="{" or c=="}":
            return "花"
        if c=="组":
            return "组";
        if c=="放":
            self.xing="放"
            return "放"
        elif c=="A" or c=="B" or c=="b":
            self.ji.append(c)
            c=self.getc()
            if c=="X" or c=="x":
                self.ji[-1]=self.ji[-1]+"X"
                return "寄"
            else:
                self.i = self.i-1
                return "寄"
        elif c=="跳":
           self.xing="跳"
           c=self.getc()
           if c=="到" or c=="转":
              return "跳"
           else:
              self.i = self.i-1
              return "跳"
        elif c=="如":
            c=self.getc()
            if c=="果":
                return "如"
            else:
                self.i = self.i-1
                return "如"
        elif c=="内":
            if self.getc()=="存":
                if self.getc()=="第":
                    pass
                else:
                    self.i = self.i-1
            else:
                self.i = self.i-1
                
            return "内"
        elif c  in "01234567890":
            self.num=int(c)
            c=self.getc()
            if c in "01234567890":
                self.num =self.num*10+int(c)
                c=self.getc()
                if c in "01234567890":
                    self.num = self.num*10+int(c)
                    self.nums.append(int(self.num))
                else:
                    self.nums.append(int(self.num))
                    self.i = self.i-1
            else:
                self.nums.append(int(self.num))
                self.i = self.i-1
            return "数"
        elif c == "入" or c =="到":
            self.xing="入"
            return "入"
        elif c ==";":
            #self.num=0
            #self.ji=""
            return "行"
        elif c =="<":
            return "小"
        elif c ==">":
            return "大"
        elif c =="=":
            return "等"
        elif c =="加":
            self.xing="加"
            return "加"
        elif c =="减":
            self.xing="减"
            return "减"
        elif c== " ":
            return ""
        else:
            if self.i==self.leng:
                print("分析完成")
            else:
                print("分析:%s%%"%(round( float(self.i)/float(self.leng)*100.,2)))
                print("分析到%s"%( self.src[self.i-1:self.i+5]))
                self.i = self.i-1
                print("遇到未编码词汇:%s"%(self.getc()))
            return "未"
    def getAllToken(self):
        T = hj.getToken()
        hj.tokens=hj.tokens+T
        while T != "未":
            T = hj.getToken()
            hj.tokens=hj.tokens+T
        return hj,tokens
    def run(self):
        return self.token2BinaralCode()
    def token2BinaralCode(self):
        T = self.getToken()
        self.tokens=self.tokens+T
        while T != "未":
            T = self.getToken()
            self.tokens=self.tokens+T
            if T=="行":
                self.getJiqi()
                print("---->寄存器:%s,操作数:%s,动作:%s,指令码:%s,%s"%(self.ji,self.num,self.xing,self.MC,self.BN))
                self.BC=self.BC+"%s,%s;"%(self.MC,self.BN)
                self.num=0
                self.ji=[]
                self.xing=""
                self.MC=""
                self.BN=""
                self.tokens=""
                print("")
        print(self.src.replace(";",";\n"))
        print(self.BC.replace(";",";\n"))
        return self.BC


if __name__=="__main__":
    '''
    #print("%s"%(src))
    #yuanma = YUANMA(src="内存第255入B;内存第BX入A;如果A<B跳2;放155入A;放128入B;")
    #yuanma = YUANMA(src=all_zhiling)
    机器码=""
    #待分析代码=循环复制代码
    待分析代码="内存第255入B;内存第BX入A;如果A<B跳2;放155入A;放128入B;"
    #yuanma = YUANMA(src=循环复制代码)
    yuanma = HuiBian2JiQi(src=待分析代码)
    T = yuanma.getToken()
    yuanma.tokens=yuanma.tokens+T
    while T != "未":
        T = yuanma.getToken()
        yuanma.tokens=yuanma.tokens+T
        #print(T ,end="")
        if T=="行":
            yuanma.getJiqi()
            print("---->寄存器:%s,操作数:%s,动作:%s,指令码:%s,%s"%(yuanma.ji,yuanma.num,yuanma.xing,yuanma.MC,yuanma.BN))
            机器码=机器码+"%s,%s;"%(yuanma.MC,yuanma.BN)
            yuanma.num=0
            yuanma.ji=[]
            yuanma.xing=""
            yuanma.MC=""
            yuanma.BN=""
            yuanma.tokens=""
            print("")
    print(待分析代码.replace(";",";\n"))
    print(机器码.replace(";",";\n"))
    '''
    待分析代码="内存第255入B;内存第BX入A;如果A<B跳2;放155入A;放128入B;"
    HJ=HuiBian2JiQi(src=待分析代码)
    HJ.run()
